package com.zzedu.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzedu.common.utils.StringUtils;
import com.zzedu.domain.ZzPictureNode;
import com.zzedu.dto.ZzPictureNodeDTO;
import com.zzedu.mapper.ZzPictureNodeMapper;
import com.zzedu.service.IZzPictureNodeService;
import com.zzedu.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * (教育)绘本节点Service业务层处理
 *
 * @author Mr.Wang
 * @date 2025-04-12
 */
@Service
public class ZzPictureNodeServiceImpl extends ServiceImpl<ZzPictureNodeMapper, ZzPictureNode> implements IZzPictureNodeService {


    @Autowired
    ISysConfigService sysConfigService;

    @Override
    public LambdaQueryWrapper<ZzPictureNode> buildQuery(ZzPictureNodeDTO zzPictureNode) {
        LambdaQueryWrapper<ZzPictureNode> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper.eq(null != zzPictureNode.getPictureBookId(), ZzPictureNode::getPictureBookId, zzPictureNode.getPictureBookId());
        lambdaQueryWrapper.eq(null != zzPictureNode.getpNodeId(), ZzPictureNode::getpNodeId, zzPictureNode.getpNodeId());
        lambdaQueryWrapper.like(StringUtils.isNotBlank(zzPictureNode.getNodeName()), ZzPictureNode::getNodeName, zzPictureNode.getNodeName());
        lambdaQueryWrapper.eq(null != zzPictureNode.getNodeType(), ZzPictureNode::getNodeType, zzPictureNode.getNodeType());
        lambdaQueryWrapper.eq(null != zzPictureNode.getSortNo(), ZzPictureNode::getSortNo, zzPictureNode.getSortNo());
        lambdaQueryWrapper.eq(null != zzPictureNode.getGmtCreate(), ZzPictureNode::getGmtCreate, zzPictureNode.getGmtCreate());
        lambdaQueryWrapper.eq(null != zzPictureNode.getGmtModify(), ZzPictureNode::getGmtModify, zzPictureNode.getGmtModify());
        lambdaQueryWrapper.eq(null != zzPictureNode.getDeleted(), ZzPictureNode::getDeleted, zzPictureNode.getDeleted());

        lambdaQueryWrapper.orderByAsc(ZzPictureNode::getSortNo).orderByAsc(ZzPictureNode::getId);
        return lambdaQueryWrapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(ZzPictureNode entity) {
        boolean res = super.save(entity);
        if (3 == entity.getNodeType()) {
            //自动保存分类
            List<String> nodeNames = JSON.parseArray(sysConfigService.selectConfigByKey("task_picture_node"), String.class);
            Long index = 0L;
            for (String nodeName : nodeNames) {
                if (null == baseMapper.selectOne(Wrappers.lambdaQuery(ZzPictureNode.class)
                        .eq(ZzPictureNode::getNodeType, 2)
                        .eq(ZzPictureNode::getpNodeId, entity.getId())
                        .eq(ZzPictureNode::getPictureBookId, entity.getPictureBookId())
                        .eq(ZzPictureNode::getNodeName, nodeName)
                        .last(" limit 1 ")
                )) {
                    ZzPictureNode zzPictureNode = new ZzPictureNode();
                    zzPictureNode.setNodeName(nodeName);
                    zzPictureNode.setPictureBookId(entity.getPictureBookId());
                    zzPictureNode.setNodeType(2);
                    zzPictureNode.setSortNo(index++);
                    zzPictureNode.setpNodeId(entity.getId());
                    baseMapper.insert(zzPictureNode);
                }
            }
        }

        return res;
    }
}